package cn.softgame.jserver.connector.tcp;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * TCP数据包接受处理类
 * @author xuquan
 * 日期:2013-5-28
 */
public class Acceptor implements Runnable{
	
	private Selector selector;
	private ServerSocketChannel ssc;
	
	public Acceptor(Selector selector,ServerSocketChannel ssc){
		this.selector=selector;
		this.ssc=ssc;
	}
	
	@Override
	public void run() {
		try {
			System.out.println("-->ready for accept!");
			SocketChannel sc = ssc.accept();
			if(sc!=null){
				sc.configureBlocking(false);
				SelectionKey sk = sc.register(selector, 0);
				sk.interestOps(SelectionKey.OP_READ);
				selector.wakeup();
				sk.attach(new TCPHandler(sk,sc));
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("accept stop!");
		}
	}

}
